package com.dianping.cat.home.alert.summary.transform;

import com.dianping.cat.home.alert.summary.entity.Alert;
import com.dianping.cat.home.alert.summary.entity.AlertSummary;
import com.dianping.cat.home.alert.summary.entity.Category;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;







public class DefaultDomMaker
  extends Object
  implements IMaker<Node>
{
  public Alert buildAlert(Node node) {
/*  23 */     String alertTime = getAttribute(node, "alertTime");
    String type = getAttribute(node, "type");
    String metric = getAttribute(node, "metric");
/*  26 */     String context = getAttribute(node, "context");
/*  27 */     String domain = getAttribute(node, "domain");
/*  28 */     String count = getAttribute(node, "count");

/*  30 */     Alert alert = new Alert();

    if (alertTime != null) {
/*  33 */       alert.setAlertTime(toDate(alertTime, "yyyy-MM-dd HH:mm:ss", null));
    }

    if (type != null) {
/*  37 */       alert.setType(type);
    }

/*  40 */     if (metric != null) {
/*  41 */       alert.setMetric(metric);
    }

/*  44 */     if (context != null) {
/*  45 */       alert.setContext(context);
    }

/*  48 */     if (domain != null) {
/*  49 */       alert.setDomain(domain);
    }

/*  52 */     if (count != null) {
/*  53 */       alert.setCount((Integer)convert(Integer.class, count, null));
    }

/*  56 */     return alert;
  }


  public AlertSummary buildAlertSummary(Node node) {
/*  61 */     String alertDate = getAttribute(node, "alert-date");
/*  62 */     String domain = getAttribute(node, "domain");

/*  64 */     AlertSummary alertSummary = new AlertSummary();

/*  66 */     if (alertDate != null) {
/*  67 */       alertSummary.setAlertDate(toDate(alertDate, "yyyy-MM-dd HH:mm:ss", null));
    }

/*  70 */     if (domain != null) {
/*  71 */       alertSummary.setDomain(domain);
    }

/*  74 */     return alertSummary;
  }


  public Category buildCategory(Node node) {
/*  79 */     String name = getAttribute(node, "name");

/*  81 */     return new Category(name);
  }




  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/*  88 */     if (value == null || value.length() == 0) {
/*  89 */       return defaultValue;
    }

/*  92 */     if (type == Boolean.class || type == boolean.class)
/*  93 */       return (T)Boolean.valueOf(value); 
/*  94 */     if (type == Integer.class || type == int.class)
      return (T)Integer.valueOf(value);
/*  96 */     if (type == Long.class || type == long.class)
/*  97 */       return (T)Long.valueOf(value); 
/*  98 */     if (type == Short.class || type == short.class)
/*  99 */       return (T)Short.valueOf(value); 
/* 100 */     if (type == Float.class || type == float.class)
/* 101 */       return (T)Float.valueOf(value); 
/* 102 */     if (type == Double.class || type == double.class)
/* 103 */       return (T)Double.valueOf(value); 
/* 104 */     if (type == Byte.class || type == byte.class)
/* 105 */       return (T)Byte.valueOf(value); 
/* 106 */     if (type == Character.class || type == char.class) {
/* 107 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 109 */     return (T)value;
  }


  protected String getAttribute(Node node, String name) {
/* 114 */     Node attribute = node.getAttributes().getNamedItem(name);

/* 116 */     return (attribute == null) ? null : attribute.getNodeValue();
  }

  protected Node getChildTagNode(Node parent, String name) {
/* 120 */     NodeList children = parent.getChildNodes();
/* 121 */     int len = children.getLength();

/* 123 */     for (int i = 0; i < len; i++) {
/* 124 */       Node child = children.item(i);

/* 126 */       if (child.getNodeType() == 1 && 
/* 127 */         child.getNodeName().equals(name)) {
/* 128 */         return child;
      }
    }


/* 133 */     return null;
  }

  protected Date toDate(String str, String format, Date defaultValue) {
/* 137 */     if (str == null || str.length() == 0) {
/* 138 */       return defaultValue;
    }

    try {
/* 142 */       return (new SimpleDateFormat(format)).parse(str);
/* 143 */     } catch (ParseException e) {
/* 144 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/alert/summary/transform/DefaultDomMaker.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */